package com.ZeroCarbon.controller;

import com.ZeroCarbon.domain.po.Account;
import com.ZeroCarbon.entity.Result;
import com.ZeroCarbon.service.IAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 登录注册控制器
 *
 * @since 2024-10-8
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/account")
public class LoginController {

    private final IAccountService loginService;

    /**
     * 注册账号
     *
     * @param account 账号信息
     */
    @PostMapping("/saveAccount")
    public Result saveAccount(@RequestBody Account account) {
        try {
            loginService.saveAccount(account);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取验证码
     */
    @GetMapping("/getVerificationCode")
    public Result getVerificationCode(@RequestParam String email) {
        try {
            loginService.getVerificationCode(email);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 校验验证码
     */
    @GetMapping("/verifyVerificationCode")
    public Result verifyVerificationCode(@RequestParam String email, @RequestParam String code) {
        try {
            loginService.verifyVerificationCode(email, code);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改密码
     *
     * @param account 账号信息
     */
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account) {
        try {
            loginService.updateAccount(account);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 登录
     *
     * @param account 账号信息
     */
    @PostMapping
    public Result login(@RequestBody Account account, HttpServletRequest request) {
        try {
            return Result.success(loginService.login(account, request));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
